package sky.wicket;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.request.mapper.parameter.PageParameters;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 21.09.13
 * Time: 15:32
 * To change this template use File | Settings | File Templates.
 */
public class MyWicketUtils {
    public static AjaxFallbackLink addExpandLink(final WebMarkupContainer container, String LinkId, String LabelId) {
        AjaxFallbackLink link = new AjaxFallbackLink(LinkId) {
            @Override
            public void onClick(AjaxRequestTarget ajaxRequestTarget) {
                container.setVisible(!container.isVisible());
                if (ajaxRequestTarget != null) {
                    ajaxRequestTarget.add(container);
                }
            }
        };
        link.add(new Label(LabelId, "[v]"));
        return link;
    }

    public static void addBookmarkablePageLinkWithLabel(MarkupContainer component, String linkId, Class<? extends WebPage> page,
                                                                        String labelId, String labelDescription) {
        BookmarkablePageLink link = new BookmarkablePageLink(linkId, page);
        link.add(new Label(labelId, labelDescription));
        component.add(link);
    }

    public static void addBookmarkablePageLinkWithLabelAndParameters(MarkupContainer component, String linkId, Class<? extends WebPage> page,
                                                                     PageParameters parameters, String labelId, String labelDescription) {
        BookmarkablePageLink link = new BookmarkablePageLink(linkId, page, parameters);
        link.add(new Label(labelId, labelDescription));
        component.add(link);
    }

    public static void addTextFieldWithLabel(MarkupContainer component, String textFieldId, String labelId,
                                             String labelDescription, boolean required) {
        TextField textField = new TextField(textFieldId);
        textField.setRequired(required);
        textField.add(new Label(labelId, labelDescription));
        component.add(textField);
    }
}
